/**
 * Created by Administrator on 2016/8/11.
 */
define(["require",'config/appregister',"business/Companyinfor/service/htttp",
	"business/services/regionService",
	"business/myResume/services/jobIntensionComp",
	"business/services/dateComp","business/services/filter","business/services/detailNavComp","jquery","business/workSkill/directive/startLevel"],function(require,app,httpServices){
    app.controller('modifyCtrl',function($scope,$stateParams,$state){
        $scope.isDetailPage = true;
        $scope.isMainpage = false;
        
		$scope.choiceAddress=$scope.areaName = $scope.regionName = "请选择"
		$scope.majorConf = {
			top:"",
			second:"",
			detail:"",
			isShow:false
		};//专业配置
		$scope.majorConf2 = {
			top:"",
			second:"",
			detail:"",
			isShow:false
		};//专业
		$scope.jobInJobConf = {
			top:"",
			detail:"",
			showValue:"",
			isShow:false,
			maxSelect:1
		};
		$scope.workExpression = {
			startValue:"",
			endValue:"",
			isShow:true
		};
			
		$scope.tags2 = [//ngInputTab自己写方法
];
		$scope.languaArr=[];
		if($stateParams.JOBcompanyId){
			function getWOrkDeatis(){
				  var req ={
                    type:"GET",
                    uri:"get",
                    data:{
                    	jobId:$stateParams.JOBcompanyId
                    }
                };
                httpServices.getJobLangularOne(req).success(function(response){
                    $scope.$apply(function(){
                    	$scope.showWOrk=response.data.type
                    	if(response.data.natureDc.sequence == 3){
						   		$scope.chec3=true;
						   		
						   }else if(response.data.natureDc.sequence == 2){
                               $scope.chec2=true;
						   }
						   $scope.chececk = response.data.natureDc.dictId;
						   $scope.oldchoiceAddress=$scope.choiceAddress=response.data.provinceObject.provinceName
						   $scope.oldregionName=$scope.regionName=response.data.regionObject.regionName
						   $scope.oldareaName=$scope.areaName=response.data.areaObject.areaName
						   $scope.oldprovinceId =$scope.provinceId=response.data.provinceObject.provinceId;
						   $scope.oldCItyId=$scope.CItyId=response.data.regionObject.regionId;
						   $scope.oldareaId=$scope.areaId=response.data.areaObject.areaId;
						   $scope.oldchoiceAddress=$scope.choiceAddress=response.data.provinceObject.provinceName
						   $scope.oldregionName=$scope.regionName=response.data.regionObject.regionName
						   $scope.oldareaName=$scope.areaName=response.data.areaObject.areaName
						   $scope.oldprovinceId =$scope.provinceId=response.data.provinceObject.provinceId;
						   $scope.oldCItyId=$scope.CItyId=response.data.regionObject.regionId;
						   $scope.oldareaId=$scope.areaId=response.data.areaObject.areaId;
						   
						   $scope.oldDeatilsAddre=$scope.DeatilsAddre=response.data.jobPlace;
						    $scope.oldshowValue=$scope.jobInJobConf.showValue=response.data.jfList[0].funcd.fdcName;
						   $scope.fdcId = response.data.jfList[0].funcd.fdcId;
						    $scope.oldtextarea=$scope.textarea=response.data.jobRequire;
						   $scope.length=response.data.jobRequire.length;
						   $scope.oldmoneyCon=$scope.moneyCon=response.data.sr.srName;
						   $scope.monId=response.data.sr.srId;
	                       $scope.oldpositionName=$scope.positionName=response.data.jobName;
	                       $scope.olddeparment=$scope.deparment=response.data.department;
	                       $scope.oldneedPeoNum=$scope.needPeoNum=response.data.jobNumber;
	                      
	                       //语言1
						   $scope.oldchoiLanone=$scope.choiLanone=response.data.jlList[0].laug.name;
						   $scope.oldchoiLanonegra=$scope.choiLanonegra=response.data.jlList[0].lgrade.name;
						   $scope.oldlangOneId=$scope.langOneId=response.data.jlList[0].laug.languageId;
						   $scope.oldlangOnegraId=$scope.langOnegraId=response.data.jlList[0].lgrade.gradeId;
						   //语言2
						   $scope.oldchoiLanTwo=$scope.choiLanTwo=response.data.jlList[1].laug.name;
						   $scope.oldchoiLanTwogra=$scope.choiLanTwogra=response.data.jlList[1].lgrade.name;
						   $scope.oldlangTwoId=$scope.langTwoId=response.data.jlList[1].laug.languageId;
						   $scope.oldlangTwograId=$scope.langTwograId=response.data.jlList[1].lgrade.gradeId;

						   
						   //地址
						   $scope.languaArr=[
								{
									"jlId":0,
									"jobId":"1",
									"languageId": $scope.langTwoId,
									"gradeId":  $scope.langTwograId
								},
								{
									"jlId":0,
									"jobId":"1",
									"languageId": $scope.langTwoId,
									"gradeId":  $scope.langTwograId
								}
							]
						   
						 
						   $scope.majorConf.detail=response.data.jpList[0].profe;
						   $scope.majorConf2.detail=response.data.jpList[1].profe;
						   $scope.padcOneId=response.data.jpList[0].profe.pdcId;
						   $scope.padcTwoId=response.data.jpList[1].profe.pdcId;
						   
						 
						   
						   $scope.oldworkYearname=$scope.workYearname=response.data.epDc.dictName;
						   $scope.oldworkYearId=response.data.epDc.dictId;
						   
						   $scope.oldworkStu=$scope.workStu=response.data.eduDc.dictName;
						   $scope.workSt=response.data.eduDc.dictId;
						   
						   
						   
						   $scope.sendTag=$scope.tags2 = response.data.jobWelfare.split(',');
						  	$scope.oldworkExpression=$scope.workExpression.startValue=response.data.endTime.split(' ')[0];
						  
						  	$scope.chececk=response.data.natureDc.dictId
						  
                    })
                });
			}
			getWOrkDeatis();
			$scope.stateGo = function(state){
				if(!state)return ;
				$state.go(state);
			};

			$scope.showMajor=function(a){
				$scope.majorConf.isShow=true;

			}
			  
			$scope.showMajor2=function(a){
				$scope.majorConf2.isShow=true;
			}
			//职能
			$scope.showWoekSkill=function(){
				$scope.jobInJobConf.isShow=true;
			}		
			$scope.sendTag=[];
            $scope.saveTagsname=function(a){
               if($scope.tags2.length<10){
        		if(a!=undefined && a != ""){
		                if($scope.tags2.indexOf(a) != -1){
		                    alert("福利不能相同哦")
		                }else{
		                    if(a.length>6){
		                        alert("每个标签不得超过6个字")
		
		                    }else{
		                        $scope.tags2.push(a);
		                        $scope.sendTag.push(a);
		                        $scope.tagsWork=""
		                    }
		
		                }
		                $scope.olda = a;
		            }

        	}else{
        		alert("不得超出10个福利")
        	}
            

            }
            $scope.deleteInput=function($index){
                $scope.tags2.splice($index,1);
                $scope.tagsWork=""
            }
			$scope.showWorkBody=4;
			function getLanguage(){
				var req ={
					type:"GET",
					uri:"getLanguage",
					data:{}
				};
				httpServices.getJobLangularOne(req).success(function(response){
					$scope.$apply(function(){
						/*console.log(JSON.stringify(response))*/
						$scope.languaOne = response.data;
					})
				});
			}
			getLanguage();
			function getLanguaOnesize(){
				var req ={
					type:"GET",
					uri:"getLanguageGrade",
					data:{}
				};
				httpServices.getJobLangularOne(req).success(function(response){
					$scope.$apply(function(){

						$scope.languaOneSize = response.data;
					})
				});
			}
			getLanguaOnesize();
			function getMap(){
				var req ={
					type:"GET",
					uri:"getProvinceList",
					data:{}
				};
				httpServices.getMap(req).success(function(response){
					$scope.$apply(function(){
						$scope.provinceCon = response.data;
					})
				});

			}
			getMap();
			function getworkYear(){//工作年限
				var req ={
					type:"GET",
					uri:"getJobExperience",
					data:{}
				};
				httpServices.getJobLangularOne(req).success(function(response){
					$scope.$apply(function(){
						$scope.workYear = response.data;
						/*console.log(response)*/
					})
				});
			}
			getworkYear();
			function getWorkStu(){//最低学历
				var req ={
					type:"GET",
					uri:"getJobEducation",
					data:{}
				};
				httpServices.getJobLangularOne(req).success(function(response){
					$scope.$apply(function(){
						$scope.Education = response.data;
					})
				});
			}
			getWorkStu();
			function getproperty(){//所有工作性质
				var req ={
					type:"GET",
					uri:"getJobNature",
					data:{}
				};
				httpServices.getJobLangularOne(req).success(function(response){
					$scope.$apply(function(){
						/*console.log(JSON.stringify(response))*/
						$scope.workproperty = response.data;
					})
				});
			}
			getproperty();
			function getConpanyInfo(){
				var req ={
					type:"GET",
					uri:"getBaseInfoByUser",
					data:{}
				};
				httpServices.getCompany(req).success(function(result){
					$scope.$apply(function(){
						$scope.compayId = result.data.companyId
					})
				});

			}
			getConpanyInfo();
			function getCommoney(){
				var req ={
					type:"GET",
					uri:"getSalaryCount",
					data:{}
				};
				httpServices.getMoney(req).success(function(response){
					$scope.$apply(function(){
						$scope.monye=response.data;
					})
				});
			}
			getCommoney();

			$scope.moneyCon=$scope.choiLanTwo = $scope.choiLanTwogra = $scope.choiLanonegra= $scope.choiLanone = "请选择";

            $scope.savelanOne=function(item){
                if(item.name == $scope.choiLanTwo){
                    alert("语言2已经选择了哦")
                }else{
                    $scope.langOneId = item.languageId;//语言1的id；
                    $scope.choiLanone = item.name;
                }

            }
            $scope.savelanTwo=function(item){
                if(item.name ==  $scope.choiLanone){
                    alert("语言1已经选择了哦")
                }else{
                    $scope.langTwoId = item.languageId;//语言2的id；
                    $scope.choiLanTwo = item.name;
                }
            }
			$scope.savelanOnegra=function(item){
				$scope.langOnegraId = item.gradeId;//语言1的等级id；
				$scope.choiLanonegra = item.name;


			}
			$scope.savelanTwogra=function(item){
				$scope.langTwograId = item.gradeId;//语言2Id
				$scope.choiLanTwogra = item.name;
			}
			$scope.saveworMoney=function(item){
				$scope.moneyId = item.srId;
				$scope.moneyCon=item.srName;
			}

			$scope.savePriceId = function(item){
				$scope.areaName = $scope.regionName = "请选择"
				$scope.provinceId = item.provinceId;//省级地质ID
				$scope.choiceAddress = item.provinceName;
				var req ={
					type:"GET",
					uri:"getRegionListInProvince",
					data:{
						provinceId:$scope.provinceId
					}
				};
				httpServices.getMap(req).success(function(response){
					$scope.$apply(function(){
						$scope.Mycity = response.data;

					})
				});

			}

			$scope.saveCItyId=function(item){
				$scope.areaName = "请选择"
				$scope.CItyId = item.regionId;
				$scope.regionName = item.regionName;
				var req ={
					type:"GET",
					uri:"getAreaListInRegionId",
					data:{
						regionId:$scope.CItyId
					}
				};
				httpServices.getMap(req).success(function(response){
					$scope.$apply(function(){
						$scope.areaCon= response.data;
						/*  console.log(JSON.stringify(response))*/
					})
				});
			}
			$scope.saveArea = function(item){
				$scope.areaId = item.areaId;
				$scope.areaName=item.areaName
			}
			$scope.workYearname ="请选择";
			$scope.saveworkYear=function(item){
				$scope.WorkYearId = item.dictId;//工作年限id
				$scope.workYearname =item.dictName;
			}
			$scope.workStu= "请选择"
			$scope.saveworStu=function(item){
				$scope.workStuId = item.dictId;//最低学历iD
				$scope.workStu = item.dictName;
			}
			/*限制textatea的字数*/
			$scope.checkText = function (textarea) {
				/*console.log(textarea.length)*/
				$scope.textareaCon = textarea;
				$scope.length = textarea.length;
				if (textarea.length > 4000) {
					textarea =textarea.substr(0,4000);
					alert("输入内容超出限制了哦！");
				}
			};
			$scope.positname=function(name){
				$scope.posiname = name;
			}
			$scope.needNum=function(num){
				$scope.PeoNum=num;
			}
			$scope.check1=function(item,$index){
				$scope.chececk = item.dictId;
				if($index==0){
                    $scope.chec0=true;
				}else if($index==1){
                    $scope.chec1=true;
				}else if($index==2){
					$scope.chec2=true;
				}else if($index==3){
					$scope.chec3=true;
				}
			}
			$scope.saveDeatailsAdd=function(a){
				$scope.DeatilsAddre = a;
			}
			$scope.saveDepar=function(deparment){
				$scope.deparment = deparment;
			}
			$scope.saveJobId=function(item){
				sessionStorage.JOBcompanyId = item.jobId;
			}
			$scope.previews=function(path){
				alert("请先发表职位")
			}
			$scope.jobId=""
			$scope.confirmSend=function(){//确认发布
				var today = new Date();//获得当前日期
				var year = today.getFullYear();//获得年份
				var month = today.getMonth() + 1;//此方法获得的月份是从0---11，所以要加1才是当前月份
				var day = today.getDate();//获得当前日期
				var startime =year+'-'+month+'-'+day
				if($scope.majorConf.detail==""){
					 if($scope.padcOneId != undefined &&  $scope.padcTwoId==undefined){
				   	$scope.marjor= [
						{"jpId":"0","pdcId":$scope.padcOneId},
					]
				   }else if($scope.padcOneId == undefined &&  $scope.padcTwoId!=undefined){
					   	$scope.marjor= [
							{"jpId":"0","pdcId":$scope.padcTwoId},
						]
				   }else if($scope.padcOneId != undefined &&  $scope.padcTwoId !=undefined){
					   	$scope.marjor= [
							{"jpId":"0","pdcId":$scope.majorConf.detail.pdcId},
							{"jpId":"0","pdcId":$scope.majorConf2.detail.pdcId}
						]
				   }
				} else{
				   	$scope.marjor= [
						{"jpId":"0","pdcId":$scope.majorConf.detail.pdcId},
						{"jpId":"0","pdcId":$scope.majorConf2.detail.pdcId}
					]
			   }
                if($scope.langOneId!=undefined && $scope.langOnegraId != undefined && $scope.langTwoId == undefined && $scope.langTwograId == undefined ){
                    $scope.languaArr=[
                        {
                            "jlId":0,
                            "jobId":"1",
                            "languageId":$scope.langOneId,
                            "gradeId":$scope.langOnegraId
                        }
                    ]
                }else if($scope.langOneId==undefined && $scope.langOnegraId == undefined
                    && $scope.langTwoId != undefined && $scope.langTwograId != undefined){
                    $scope.languaArr=[
                        {
                            "jlId":0,
                            "jobId":"1",
                            "languageId": $scope.langTwoId,
                            "gradeId":  $scope.langTwograId
                        }

                    ]
                }else {
                    $scope.languaArr=[
                        {
                            "jlId":0,
                            "jobId":"1",
                            "languageId":$scope.langOneId,
                            "gradeId":$scope.langOnegraId
                        }, {
                            "jlId": 0,
                            "jobId": "1",
                            "languageId": $scope.langTwoId,
                            "gradeId": $scope.langTwograId
                        }
                    ]

                }

				var marjors = JSON.stringify($scope.marjor);
				var langualge =JSON.stringify($scope.languaArr)
				if($scope.jobInJobConf.showValue=="" &&$scope.fdcId==undefined){
					alert("请选择职能")
				}else if($scope.oldshowValue==$scope.jobInJobConf.showValue){
					$scope.jobId= $scope.fdcId;
				}else{
					$scope.jobId=$scope.jobInJobConf.detail[0].fdcId
				}
				if($scope.olddeparment==$scope.deparment){
					$scope.deparment=$scope.olddeparment;
				}
				if($scope.oldneedPeoNum==$scope.needPeoNum){
					$scope.PeoNum=$scope.oldneedPeoNum;
				}else if($scope.PeoNum == undefined | $scope.PeoNum == null){
					alert("请输入招聘人数")
				}
				if($scope.oldtextarea==$scope.textarea){
					$scope.textareaCon=$scope.oldtextarea;
				} 
				if($scope.oldlangOneId==$scope.langOneId){
					$scope.langOneId=$scope.oldlangOneId;
				}
				if($scope.oldlangOnegraId==$scope.langOnegraId){
					$scope.langOnegraId=$scope.oldlangOnegraId;
				}
				if($scope.oldlangTwoId==$scope.langTwoId){
					$scope.langTwoId=$scope.oldlangTwoId;
				}
				if($scope.oldlangTwograId==$scope.langTwograId){
					$scope.langTwograId=$scope.oldlangTwograId;
				}
				if($scope.oldprovinceId==$scope.provinceId){
					$scope.provinceId=$scope.oldprovinceId
				}
				if($scope.oldCItyId==$scope.CItyId){
					$scope.CItyId=$scope.oldCItyId
				}
				if($scope.oldareaId==$scope.areaId){
					$scope.areaId=$scope.oldareaId
				}
				if($scope.oldDeatilsAddre==$scope.DeatilsAddre){
					$scope.DeatilsAddre=$scope.oldDeatilsAddre
				}
				
				if($scope.oldworkYearname==$scope.workYearname){
					$scope.WorkYearId=$scope.oldworkYearId;
				}
				if($scope.oldworkStu==$scope.workStu){
					$scope.workStuId=$scope.workSt;
				}
				if($scope.oldmoneyCon==$scope.moneyCon){
					$scope.moneyId= $scope.monId
				}
				if($scope.oldworkExpression==$scope.workExpression.startValue){
					$scope.workExpression.startValue=$scope.oldworkExpression;
				}
				if($scope.oldpositionName==$scope.positionName){
					$scope.posiname=$scope.oldpositionName
				}else if($scope.posiname == ""){
					alert("请输入职位名称");
				}
				if($scope.posiname == undefined){
                	alert("请输入职位名称");
	            }else if($scope.PeoNum == undefined){
	                alert("请输入招聘人数")
	            }else if($scope.choiceAddress=="请选择" | $scope.regionName=="请选择" | $scope.areaName=="请选择" ){
	                alert("请选择工作地点");
	            }else if($scope.chececk==undefined){
	                alert("请选择工作性质")
	            }else if($scope.length==undefined || $scope.length==0){
	            	 alert("请输入职位描述")
	            }else if($scope.jobInJobConf.showValue==""){
	            	alert("请输入职能")
	            }else if($scope.moneyId == undefined){
	            	alert("请选择薪资")
	            }else{
					var req ={
						type:"POST",
						uri:"update",
						data:{
                            jobId:$stateParams.JOBcompanyId,
							jobName:$scope.posiname,
							companyId:$scope.compayId,//$scope.compayId
							jobNumber:$scope.PeoNum,
							provinceId:$scope.provinceId,
							regionId:$scope.CItyId,
							areaId:$scope.areaId,
							jobPlace:$scope.DeatilsAddre,
							functionsDetailIds:$scope.jobId,//职能详情
							experienceId:$scope.WorkYearId,//工作年限id
							educationId:$scope.workStuId,//最低学历id
							natureId:$scope.chececk,//工作性质
							salaryId:$scope.moneyId,//薪资id
							jobWelfare:$scope.sendTag.join(","),//福利
							startTime:startime,
							endTime:$scope.workExpression.startValue==""?"":$scope.workExpression.startValue.replace("/","-").replace("/","-"),
							jobRequire:$scope.textareaCon,
							department:$scope.deparment,
							languageJsonStr:langualge,
							profeJsonStr:marjors
						}

					};
					httpServices.getJobLangularOne(req).success(function(response){
						$scope.$apply(function(){
							$scope.areaCon= response.data;
							if(response.msg == "success"){
								alert("修改职位成功")
								$scope.jobId = response.data;
								$state.go("comIndexCtrl.workPosCtrl")
							}

						})
					});

		
				}


			}
			/*$scope.jobId*/
			$scope.previewWOrk=function(){
				if($scope.jobId != undefined){
					$state.go("companyDetailsCtrl",{modeifyWorkId:$stateParams.JOBcompanyId})
				}else{
					alert("请先发布职位")
				}
			}
}

    });
});

